package com.twotiger.and.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.bean.Coupon;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.StringUtils;

/**
 * 优惠券适配器
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月18日 下午12:12:35
 */
@SuppressLint({ "NewApi", "ResourceAsColor" })
public class DealCouponAdapter extends BaseAdapter {
    private Activity mcontext;
    private ArrayList<Coupon> mlist;
    private BaseHandler mhandler;
    private int ON_SELECTED_MSG;

    public DealCouponAdapter(Activity _Activity, ArrayList<Coupon> coupons, BaseHandler handler,
                             int ON_SELECTED_MSG) {
        super();
        this.mcontext = _Activity;
        this.mhandler = handler;
        this.mlist = coupons;
        this.ON_SELECTED_MSG = ON_SELECTED_MSG;
    }

    public void setSelected(Coupon coupon) {
        for (Coupon c : mlist) {
            c.isSelected = false;
        }
        coupon.isSelected = true;
    }

    public Coupon getSelected() {
        for (Coupon c : mlist) {
            if (c.isSelected)
                return c;
        }
        return null;
    }

    public void resetData(ArrayList<Coupon> _List) {
        mlist.clear();
        if (!ListUtils.isEmpty(_List)) {
            mlist.addAll(_List);
        }
        notifyDataSetChanged();
    }

    public void addData(ArrayList<Coupon> _List) {
        if (!ListUtils.isEmpty(_List)) {
            mlist.addAll(_List);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mlist)) {
            return mlist.size();
        }
        return 0;

    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MViewholder holder = null;
        if (convertView == null) {
            LayoutInflater minflater = LayoutInflater.from(mcontext);
            convertView = minflater.inflate(R.layout.coupon_list_item, null);
            holder = getViewHolder(convertView);
        } else {
            holder = (MViewholder) convertView.getTag();
        }
        holder.prepare();
        int _position = position;
        Coupon coupon = mlist.get(_position);
        holder.fillCoupon(coupon);
        return convertView;
    }

    public MViewholder getViewHolder(View convertView) {
        MViewholder holder = new MViewholder();
        holder.coupon_item = ViewHolder.get(convertView, R.id.coupon_item);
        holder.coupon_val = ViewHolder.get(convertView, R.id.coupon_val);
        holder.coupon_val_suffix = ViewHolder.get(convertView, R.id.coupon_val_suffix);
        holder.coupon_type_name = ViewHolder.get(convertView, R.id.coupon_type_name);
        holder.coupon_info = ViewHolder.get(convertView, R.id.coupon_info);
        holder.coupon_end_time = ViewHolder.get(convertView, R.id.coupon_end_time);
        holder.use_btn = ViewHolder.get(convertView, R.id.use_btn);
        holder.coupon_name = ViewHolder.get(convertView, R.id.coupon_name);
        convertView.setTag(holder);
        return holder;
    }

    class MViewholder {
        /**item 背景*/
        LinearLayout coupon_item;
        /**值*/
        TextView coupon_val;
        /**后缀*/
        TextView coupon_val_suffix;
        /**类型名称*/
        TextView coupon_type_name;
        /**信息:预计收益、使用条件*/
        TextView coupon_info;
        /**截至时间*/
        TextView coupon_end_time;
        /**使用按钮*/
        Button use_btn;
        /**优惠券活动名称*/
        TextView coupon_name;
        
        /**
         * 初始化基本属性
         */
        public void prepare() {

        }

        public void setColor(int t1, int t2) {
            coupon_type_name.setTextColor(t2);
            coupon_info.setTextColor(t2);
            coupon_end_time.setTextColor(t2);
            coupon_val.setTextColor(t1);
            use_btn.setTextColor(t2);
            coupon_val_suffix.setTextColor(t2);
            coupon_name.setTextColor(t2);
        }

        public void fillCoupon(final Coupon coupon) {
            coupon_name.setText(coupon.getName());
            coupon_type_name.setText(coupon.getAwardTypeName());
            coupon_info.setText(StringUtils.subString(coupon.getUseConditionDesc(), 20));
            coupon_end_time
                .setText(DateUtil.format(
                    DateUtil.getDateByLong(Long.parseLong(coupon.getInvalidTime())),
                    "yyyy-MM-dd HH:mm") + " 前使用");
            coupon_val.setText(coupon.getAwardValue() + "");
            coupon_val_suffix.setText(coupon.getAwardValueSuffix());
            int status = coupon.getStatus();
            int type = coupon.getAwardType();
            use_btn.setEnabled(false);
            if (status == 0) { //未使用需要放开功能按钮
                use_btn.setBackgroundDrawable(null);
                use_btn.setEnabled(true);
                use_btn.setText("使用");
            } else if (status == 3) {//使用中，因为代金券和加息券的使用中状态即为未使用状态，故把红包和他俩倬区分
                if(type == 1 || type ==4){ //代金券和加息券
                    use_btn.setBackgroundDrawable(null);
                    use_btn.setEnabled(true);
                    use_btn.setText("使用");
                }else { //红包
                    use_btn.setBackgroundDrawable(mcontext.getResources().getDrawable(btn[status]));
                    use_btn.setEnabled(false);
                    use_btn.setText("");
                }
                
            } else if (btn[status] > 0) {//未使用
                use_btn.setBackgroundDrawable(mcontext.getResources().getDrawable(btn[status]));
                use_btn.setEnabled(false);
                use_btn.setText("");
            }
            int bgId = R.drawable.dikouquan_unused;
            int color1 = R.color.coupon_daijin_1;
            int color2 = R.color.coupon_daijin_2;

            if (type < itemBg.length && status < itemBg[type].length) {
                bgId = itemBg[type][status];
                color1 = textC1[type][status];
                color2 = textC2[type][status];
            }

            LogUtil
                .info(DealCouponAdapter.class, "type : " + type + ",status:" + status + ",bgId:"
                                               + bgId + ",color1:" + color1 + ",color2" + color2);
            coupon_item.setBackgroundDrawable(mcontext.getResources().getDrawable(bgId));
            setColor(mcontext.getResources().getColor(color1),
                mcontext.getResources().getColor(color2));
            use_btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelected(coupon);
                    mhandler.sendEmptyMessage(ON_SELECTED_MSG);
                }
            });

        }

    }

    private static int[][] itemBg;
    private static int[][] textC1;
    private static int[][] textC2;
    private static int[] btn;//状态聚合
    static {
        //初始化不同类型、状态的数据
        //类型、状态

        //0 现金、1加息卷、4代金券、5免审红包
        //0 未使用、1 已经使用、2已过期、3使用中、4作废

        itemBg = new int[6][5];
        textC1 = new int[6][5];
        textC2 = new int[6][5];
        btn = new int[5];

        btn[0] = 0;
        btn[1] = R.drawable.yinzhang_1;
        btn[2] = R.drawable.yinzhang_3;
        btn[3] = R.drawable.yinzhang_2;
        btn[4] = R.drawable.yinzhang_4;

        //现金
        itemBg[0][0] = R.drawable.xianjinhongbao_unused; //现金 == 红包
        itemBg[0][1] = R.drawable.xianjinhongbao_used;
        itemBg[0][2] = R.drawable.all_expire;
        itemBg[0][3] = R.drawable.xianjinhongbao_used;
        itemBg[0][4] = R.drawable.all_expire;

        textC1[0][0] = R.color.coupon_hongbao_1;
        textC1[0][1] = R.color.coupon_hongbao_used_1;
        textC1[0][2] = R.color.coupon_expire_1;
        textC1[0][3] = R.color.coupon_hongbao_used_1;
        textC1[0][4] = R.color.coupon_hongbao_1;

        textC2[0][0] = R.color.coupon_hongbao_2;
        textC2[0][1] = R.color.coupon_hongbao_used_2;
        textC2[0][2] = R.color.coupon_expire_2;
        textC2[0][3] = R.color.coupon_hongbao_used_2;
        textC2[0][4] = R.color.coupon_expire_2;

        //加息券
        itemBg[1][0] = R.drawable.jiaxiquan_unused;
        itemBg[1][1] = R.drawable.jiaxiquan_used;
        itemBg[1][2] = R.drawable.all_expire;
        itemBg[1][3] = R.drawable.jiaxiquan_unused;
        itemBg[1][4] = R.drawable.all_expire;

        textC1[1][0] = R.color.coupon_jiaxi_1;
        textC1[1][1] = R.color.coupon_jiaxi_used_1;
        textC1[1][2] = R.color.coupon_expire_1;
        textC1[1][3] = R.color.coupon_jiaxi_1;
        textC1[1][4] = R.color.coupon_hongbao_1;

        textC2[1][0] = R.color.coupon_jiaxi_2;
        textC2[1][1] = R.color.coupon_jiaxi_used_2;
        textC2[1][2] = R.color.coupon_expire_2;
        textC2[1][3] = R.color.coupon_jiaxi_2;
        textC2[1][4] = R.color.coupon_expire_2;

        //代金券
        itemBg[4][0] = R.drawable.dikouquan_unused; //抵扣==代金
        itemBg[4][1] = R.drawable.dikouquan_used;
        itemBg[4][2] = R.drawable.all_expire;
        itemBg[4][3] = R.drawable.dikouquan_unused;
        itemBg[4][4] = R.drawable.all_expire;

        textC1[4][0] = R.color.coupon_daijin_1;
        textC1[4][1] = R.color.coupon_daijin_used_1;
        textC1[4][2] = R.color.coupon_expire_1;
        textC1[4][3] = R.color.coupon_daijin_1;
        textC1[4][4] = R.color.coupon_daijin_1;

        textC2[4][0] = R.color.coupon_daijin_2;
        textC2[4][1] = R.color.coupon_daijin_used_2;
        textC2[4][2] = R.color.coupon_expire_2;
        textC2[4][3] = R.color.coupon_daijin_2;
        textC2[4][4] = R.color.coupon_expire_2;

        //免审红包   此样式和代金券是一模一样的  所有把代金券的复制下来一份

        itemBg[5][0] = R.drawable.xianjinhongbao_unused; //现金 == 红包
        itemBg[5][1] = R.drawable.xianjinhongbao_used;
        itemBg[5][2] = R.drawable.all_expire;
        itemBg[5][3] = R.drawable.xianjinhongbao_used;
        itemBg[5][4] = R.drawable.all_expire;

        textC1[5][0] = R.color.coupon_hongbao_1;
        textC1[5][1] = R.color.coupon_hongbao_used_1;
        textC1[5][2] = R.color.coupon_expire_1;
        textC1[5][3] = R.color.coupon_hongbao_used_1;
        textC1[5][4] = R.color.coupon_hongbao_1;

        textC2[5][0] = R.color.coupon_hongbao_2;
        textC2[5][1] = R.color.coupon_hongbao_used_2;
        textC2[5][2] = R.color.coupon_expire_2;
        textC2[5][3] = R.color.coupon_hongbao_used_2;
        textC2[5][4] = R.color.coupon_expire_2;

    }
}
